package dao;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import parsers.CommonMethods;
import parsers.teacher.DepartmentsParser;
import parsers.teacher.TeacherParser;
import parsers.teacher.TeachersParser;

import model.teacher.Department;
import model.teacher.Teacher;

public class TeacherDAO 
{
	private static Map<String, Department> departmentsCP = new HashMap<String, Department>();
	private static Map<String, Department> departmentsWithTeachersCP = new HashMap<String, Department>();
	private static Map<String, Teacher> teachersScheduleCP = new HashMap<String, Teacher>();
	
	private static String currentDate = util.Utilities.getDateTime();
	
	private static String[] file = new String[] {makeFileName("depCP"), makeFileName("dwtCP"), makeFileName("tscCP")};
	
	private long startTime;
	
	@SuppressWarnings("unchecked")
	public TeacherDAO()
	{
		startTime();
		System.out.print("Initialising Teacher->Departments: ");
		if( (new File(file[0])).exists() )
		{
			System.out.print("(find serialized file " + file[0] + ") ");
			departmentsCP = (Map<String, Department>) util.Utilities.deserialize(file[0]);
		}
		else
		{
			initDepartments();
		}
		System.out.println(countTime() + " ms");
		
		/////////////////////////////////////////////
		
		startTime();
		System.out.print("Initialising Teacher->Teachers: ");
		if( (new File(file[1])).exists() )
		{
			System.out.print("(find serialized file " + file[1] + ") ");
			departmentsWithTeachersCP = (Map<String, Department>) util.Utilities.deserialize(file[1]);
		}
		else
		{
			initTeachers();
		}
		System.out.println(countTime() + " ms");
		
		/////////////////////////////////////////////
		
		startTime();
		System.out.print("Initialising Teacher->TeachersSchedule: ");
		if( (new File(file[2])).exists() )
		{
			System.out.print("(find serialized file " + file[2] + ") ");
			teachersScheduleCP = (Map<String, Teacher>) util.Utilities.deserialize(file[2]);
		}
		else
		{
			initTeachersSchedule();
		}
		System.out.println(countTime() + " ms");
	}
	
	private static String makeFileName(String name)
	{
		return Linker.path + TeacherDAO.class.getName() + name + currentDate + ".jsr";
	}
	
	public static void serialize()
	{
		if( !(new File(file[0])).exists() )
		{
			util.Utilities.serialize(departmentsCP, file[0]);
		}
		
		if( !(new File(file[1])).exists() )
		{
			util.Utilities.serialize(departmentsWithTeachersCP, file[1]);
		}
		
		if( !(new File(file[2])).exists() )
		{
			util.Utilities.serialize(teachersScheduleCP, file[2]);
		}
	}
	
	public static Map<String, Department> getDepartmentsCP()
	{
		return departmentsCP;
	}

	public static Map<String, Department> getDepartmentsWithTeachersCP()
	{
		return departmentsWithTeachersCP;
	}

	public static Map<String, Teacher> getTeachersScheduleCP()
	{
		return teachersScheduleCP;
	}

	private void initDepartments()
	{
		DepartmentsParser dtp = new DepartmentsParser();
		ArrayList<Department> departments = null;
		
		try
		{
			departments = dtp.parse(new URL("http://plan.uz.zgora.pl/nauczyciel_lista_wydzialow.php?pTyp=P"));
			
			for (int i = 0; i < departments.size(); i++)
			{
				departmentsCP.put(departments.get(i).getId(), departments.get(i));
			}
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		} 
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}
	
	private void initTeachers()
	{
		Iterator<Department> departments = departmentsCP.values().iterator();
		while(departments.hasNext())
		{
			Department department = departments.next();
			Department dept = new Department(department.getName());
			dept.setLink(CommonMethods.parseLink(department.getLink()));
			dept.setId(department.getId());
			
			TeachersParser tp = new TeachersParser(dept);
			
			try
			{
				tp.parse(true);
				departmentsWithTeachersCP.put(dept.getId(), dept);
			} 
			catch (IOException e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void initTeachersSchedule()
	{
		Iterator<Department> departments = departmentsWithTeachersCP.values().iterator();
		while(departments.hasNext())
		{
			Department department = departments.next();
			ArrayList<Teacher> teachers = department.getTeachers();
			
			for (int i = 0; i < teachers.size(); i++)
			{
				Teacher teacher = new Teacher(teachers.get(i).getName(), teachers.get(i).getUnit());
				teacher.setId(teachers.get(i).getId());
				teacher.setLink(CommonMethods.parseLink(teachers.get(i).getLink()));
				
				try
				{
					TeacherParser tp = new TeacherParser(teacher);
					tp.parse(true);
					teachersScheduleCP.put(teacher.getId(), teacher);
				} 
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
	
	private void startTime()
	{
		startTime = System.currentTimeMillis();
	}
	
	private long countTime()
	{
		return System.currentTimeMillis() - startTime;
	}
}